package com.huangdada.hfweather.controller;

import com.huangdada.hfweather.service.CityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Description: 城市的Controller
 * @PROJECT_NAME: hf-weather
 * @CLASS_NAME:
 * @Author: huanglibin
 * @Date: 2020-03-31 20:42
 * @Version: 1.0
 */
@Slf4j
@Controller
public class CityController {
    @Autowired
    private CityService cityService;

    @RequestMapping(value="/importCitys",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String importCitys(@RequestParam("fileName") MultipartFile file){
        //判断文件名是否为空
        if(file == null){
            log.info("文件名错误....");
            return null;
        }

        //获取文件名
        String name = file.getOriginalFilename();

        //判断文件大小、名称
        long size = file.getSize();
        if (name==null || ("").equals(name) && size==0){
            log.info("获取不到文件名称...");
            return  null;
        }

        try {
            //把文件转换成字节流形式
            InputStream in = file.getInputStream();
            cityService.importCityAll(name,file);
        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败！";
        }
        return "上传成功";
    }
}
